package jp.ddo.trismegistos.filter;

import java.io.IOException;

import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

/**
 * すべてのリクエストに対するフィルター。<br>
 * 「.」を含むリクエストに対しては何も行わず、<br>
 * 含まないリクエストに対しては、Actionとみなしてforwardを行う。
 * 
 * @author y_sugasawa
 * @since 2011/06/05
 * 
 */
@WebFilter(filterName = "WebFilterTest", urlPatterns = "/*", dispatcherTypes = { DispatcherType.REQUEST })
public class RoutingFilter implements Filter {

	/** ログ */
	private static final Logger log = Logger.getLogger(RoutingFilter.class
			.getName());

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain filterChain) throws IOException, ServletException {
		final String requestUrl = ((HttpServletRequest) request)
				.getRequestURI();
		log.debug("フィルター：" + requestUrl);
		if (requestUrl.matches(".*[\\.].*")) {
			filterChain.doFilter(request, response);
		} else {
			((HttpServletRequest) request).getRequestDispatcher(
					requestUrl + ".do").forward(request, response);
		}
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

}
